<template>
  <div class="wrap">
    <div class="d-flex">
      <query-list
          v-model="params.massAttributeElements"
          :description="queryListDescription"
          :tipMsg="queryListDescription"
          @removeItem="removeSelect"/>
    </div>
    <div class="d-flex align-items-center" v-if="massCover">
      <el-checkbox v-model="isCovMass" @change="changeMassCover"
                   :label="$t('message.MassAttributeTool.coverageQuality')"/>
      <base-input-number v-model="massValue"
                         :min="0.0"
                         :disabled="!isCovMass"
                         @change="confirmModal()">
      </base-input-number>
      <label> g</label>
      <el-button type="primary" size="small"
                 :disabled="!isCovMass"
                 @click="reSet">{{ $t('message.MassAttributeTool.reset') }}
      </el-button>
    </div>
    <div v-if="params.massAttributeElements.length > 0 && massAttributeInfo" class="border massAttributeMessageBox"
         tabindex="-1">
      <mass-attribute-info-content v-if="!!massAttributeInfo"
                                   :mass-attribute-info="massAttributeInfo"/>
    </div>
    <div class="btn">
      <el-button type="primary" size="small"
                 :disabled="!isCompute"
                 @click="computeMassAttribute">{{ $t('message.MassAttributeTool.compute') }}
      </el-button>
      <el-button size="small"
                 @click="cancel">{{ $t('message.MassAttributeTool.close') }}
      </el-button>
    </div>

  </div>
</template>

<script>

import {massAttributeCommand} from "../../../Command/MassAttributeCommand";
import {ElementType, EntityType} from "../../../modeling/TypeDefine";
import MassAttributeInfoContent from "./MassAttributeInfoContent.vue";
import {massAttributeToolManager} from "./MassAttributeToolManager";
import DialogMixin from "../DialogMixin";

export default {
  mixins: [DialogMixin],
  name: "CmdMassAttribute",
  components: {MassAttributeInfoContent},
  data() {
    return {
      massAttributeInfo: null,//质量属性列表
      massAttributeMessage: '',//质量属性展示文本
      disableCoverBtn: true,
      workspaceMode: '',
      isInit: false,
      isCovMass: massAttributeToolManager.isCovMass,
      massValue: massAttributeToolManager.massValue,
      params: {
        massAttributeElements: [],//元素列表
        selectElements: [],//选择的element
        queryListActiveMap: {
          massAttributeElements: 1,
        },
      },
    }
  },

  mounted() {
    if (!this.isInit) {
      massAttributeCommand.init(600, 300, 550, () => {
        massAttributeCommand.clearAllHighLight();
        if (this.workspaceMode.length === 0) {
          let params = [];
          massAttributeCommand.getWorkspaceMode(params, (data) => {
            this.workspaceMode = data;
            this.pickMode();
          });
        }
        this.isInit = true;
      });
    }
  },

  created() {
  },

  methods: {
    pickMode() {
      let filterSelectTypes = [ElementType.FACE, ElementType.SURFACE];
      massAttributeCommand.setSelectType(filterSelectTypes);
      if (this.workspaceMode === 'PartDocument') {
        // 三维区域选择处理
        massAttributeCommand.handlePickElement((elements) => {
          elements?.element?.forEach(entity => {
            this.params.selectElements.splice(0);
            this.params.selectElements.push({elementId: entity?.element?.id});
            massAttributeCommand.getEntitiesByIds([entity?.element?.params?.entityRefId], (res) => {
              if (res?.length > 0 && res[0].type === EntityType.SOLID) {
                let entityInfo = {
                  name: res[0]?.name,
                  type: res[0]?.type,
                  id: res[0]?.id
                };
                this.addEntityToCompute(entityInfo);
              }
            });
          })
        });

        // 实体树选择处理
        massAttributeCommand.handlePickEntityItem(res => {
          if (res?.entities?.length > 0) {
            this.addEntityToCompute(res?.entities[0]);
          }
        });
      } else if (this.workspaceMode === 'AssemblyDocument') {
        // 三维区域选择处理
        massAttributeCommand.handlePickElement((elements) => {
          this.params.selectElements.splice(0);
          if (elements?.element?.length > 0) {
            let eleId = elements?.element[0]?.element.id;
            let current = {
              instanceId: elements?.element[0]?.instance?.id,
              elementId: elements?.element[0]?.element?.id
            };
            this.params.selectElements.push(current);
          }

          elements?.element.forEach(elementInstance => {
            let instance = elementInstance.instance;
            let InstanceInfo = {
              name: instance.name,
              type: "",
              instanceId: instance.id,
            };
            this.addInstanceToCompute(InstanceInfo);
          })
        });

        // 实例树选择处理
        massAttributeCommand.handlePickInstance(res => {
          if (res?.instance?.length > 0) {
            this.addInstanceToCompute({
              instanceId: res?.instance[0]?.id,
              name: res?.instance[0]?.name,
              type: res?.instance[0]?.type
            });
          }
        });
      }
    },

    removeSelect(item) {
      if (this.workspaceMode === 'PartDocument') {
        massAttributeCommand.clearEntityOutline(item.id);
      } else if (this.workspaceMode === 'AssemblyDocument') {
        massAttributeCommand.clearHighLightInstances([item.instanceId]);
      }
      if (this.params.massAttributeElements.length > 0) {
        this.computeMassAttribute();
      } else {
        this.massAttributeInfo = null;
      }
    },

    reSet() {
      let instanceId = this.params.massAttributeElements[0].instanceId;
      let massCoverParams = {
        instanceId: instanceId
      }
      massAttributeCommand.massCoverReSet(massCoverParams);
      this.computeMassAttribute();
      this.isCovMass = false;
    },

    confirmModal() {
      let instanceId = this.params.massAttributeElements[0].instanceId;
      let massCoverValue = this.massValue;
      let massCoverParams = {
        instanceId: instanceId,
        massValue: massCoverValue
      }
      massAttributeCommand.massCoverConfirm(massCoverParams);
      this.computeMassAttribute();
    },

    computeMassAttribute() {
      this.massAttributeInfo = null;
      massAttributeCommand.getMassAttributeInfo(this.params.massAttributeElements, (data) => {
        //Todo,返回结果处理
        massAttributeToolManager.parseMassAttributeInfos(data);
        this.massValue = massAttributeToolManager.massValue;
        this.isCovMass = massAttributeToolManager.isCovMass;
        this.massAttributeInfo = massAttributeToolManager.massAttributeInfo;
      });
    },

    changePluginContainerHeight() {
      massAttributeCommand.changePluginContainerHeight({
        minWidth: this.$el.clientWidth,
        maxHeight: this.$el.clientHeight
      })
    },

    cancel() {
      for (let i = 0; i < this.params.massAttributeElements.length; i++) {
        if (this.workspaceMode === 'PartDocument') {
          massAttributeCommand.clearEntityOutline(this.params.massAttributeElements[i].id);
        } else if (this.workspaceMode === 'AssemblyDocument') {
          massAttributeCommand.clearHighLightInstances([this.params.massAttributeElements[i].instanceId]);
        }
      }
      if (this.params.massAttributeElements.length > 0) {
        this.params.massAttributeElements = [];
      }
      if (this.massAttributeInfo) {
        this.massAttributeInfo = null;
      }
      massAttributeCommand.clearAllHighLight();
      massAttributeCommand.closeActiveCmd();
    },

    changeMassCover(value) {
      this.isCovMass = value;
      if (!this.isCovMass) {
        this.reSet();
        this.massValue = massAttributeToolManager.massValue;
      } else {
        this.massValue = massAttributeToolManager.massValue;
      }
    },

    addEntityToCompute(entity) {
      let findIndex = this.params.massAttributeElements.find(item => {
        return item.id === entity.id;
      });
      if (!findIndex) {
        this.params.massAttributeElements.push(entity);
        //massAttributeCommand.clearHighLightElements([entity.id]);
        this.computeMassAttribute();
      }
    },

    addInstanceToCompute(instance) {
      if (!instance?.instanceId) {
        return;
      }
      let isSameInstance = this.params.massAttributeElements.some((item) => instance.instanceId === item.instanceId);
      if (isSameInstance) {
        //massAttributeCommand.clearHighLightInstanceElement(paramId);
        let instanceIds = this.params.massAttributeElements.map(e => e.instanceId);
        if (instanceIds) {
          massAttributeCommand.highLightInstances(instanceIds);
        }
        return;
      }
      //massAttributeCommand.clearHighLightInstanceElement(paramId);
      this.params.massAttributeElements.push(instance);
      this.computeMassAttribute();
    }
  },

  watch: {
    "params.massAttributeElements": {
      handler(elements) {
        if (this.workspaceMode === 'PartDocument') {
          let entIds = elements.map(e => e.id);
          if (entIds) {
            massAttributeCommand.highLightEntityOutline(entIds);
          }
        } else if (this.workspaceMode === 'AssemblyDocument') {
          let instanceIds = elements.map(e => e.instanceId);
          if (instanceIds) {
            massAttributeCommand.highLightInstances(instanceIds);
          }
        }
      }
    },

    // 用于处理选择面高亮体后，消除面的高亮
    "params.selectElements": {
      handler(elements) {
        if (this.workspaceMode === 'PartDocument') {
          if (elements && elements?.length > 0) {
            massAttributeCommand.clearHighLightElements([elements[0].elementId]);
          }
        } else if (this.workspaceMode === 'AssemblyDocument') {
          if (elements && elements?.length > 0) {
            massAttributeCommand.clearHighLightInstanceElement(elements[0]);
          }
        } else {
        }
      }
    }
  },

  computed: {
    isCompute() {
      return this.params.massAttributeElements.length === 0 ? false : true;
    },

    isPartDocument() {
      if (this.workspaceMode?.length > 0) {
        return this.workspaceMode === 'PartDocument';
      }
    },

    queryListDescription() {
      return this.isPartDocument ? this.$t('message.MassAttributeTool.selectSolids') : this.$t('message.MassAttributeTool.selectInstances');
    },

    queryListTipMsg() {
      return this.isPartDocument ? this.$t('message.MassAttributeTool.TipMsgSolids') : this.$t('message.MassAttributeTool.TipMsgInstances');
    },

    massCover() {
      return (!this.isPartDocument && this.params.massAttributeElements.length === 1 && this.massAttributeInfo) ? true : false;
    },
  },
}
</script>

<style scoped>
.wrap {
  font-size: 12px;
}

</style>
